﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Logic;
using System.IO;
using System.Data;

namespace EasyCheque.Cheque
{
    public partial class UseCheque : System.Web.UI.Page
    {
        public Stream CQFileStream;
        protected void Page_Load(object sender, EventArgs e)
        {
            Table_ChequePay.Visible = false;
            ChequeImage.Visible = false;
            Table_CommitMessage.Visible = false;
        }
        protected void AccTextChanged(object sender, EventArgs e)
        {
            if (this.TextBox_PayeeAccount.Text.Length == 19)
            {
                string C_NAME = Logic.Logic.QueryAccount(TextBox_PayeeAccount.Text);
                if (C_NAME == "")
                {
                    Label_AccInfo.Text = "账号不存在";
                }else{
                    Label_AccInfo.Text = "开户人姓名：" + C_NAME;
                }
            }
        }
        protected void PayButtonClick(object sender, EventArgs e)
        {
            Logic.Logic.m_ExceptionHandle.AlarmOff();
            if (Session["ChequeFileStream"] != null)
            {
                Table_CommitMessage.Visible = false;
                if (Logic.Logic.QueryAccount(TextBox_PayeeAccount.Text) == "")
                {
                    Label_CommitMessage.Text = "收票账户不存在";
                    Table_CommitMessage.Visible = true;
                    return;
                }
                ((Stream)Session["ChequeFileStream"]).Position = 0;
                string UseRe = Logic.Logic.UseCheque((Stream)Session["ChequeFileStream"], TextBox_PayKey.Text, TextBox_PayeeAccount.Text);
                if (UseRe != "")
                {
                    Label_CommitMessage.Text = UseRe;
                    Table_CommitMessage.Visible = true;
                    return;
                }
                Session["ChequeFileStream"] = null;
                Label_CommitMessage.Text = "";
                Table_CommitMessage.Visible = false;
                Table_ChequePay.Visible = false;
                ChequeImage.Visible = false;
                Session["operationDone"] = "YES";
                Response.Redirect("~/Cheque/Hello_Cheque.aspx");
            }
        }
        protected void LoadCheque(object sender, EventArgs e)
        {
            if (FileUpload_ChequeFile.HasFile)
            {
                ChequeImage.Visible = true;
                Table_ChequePay.Visible = false;
                Table_CommitMessage.Visible = true;
                Label_CommitMessage.Text = "";
                TextBox_PayeeAccount.Enabled = true;
                CQFileStream = FileUpload_ChequeFile.FileContent;
                Logic.Cheque cf = new Logic.Cheque(CQFileStream);
                int ChequeState = cf.Verify();
                if (ChequeState == -1)
                {
                    Label_CommitMessage.Text = "支票文件验证失败";
                    ChequeImage.Visible = false;
                    return;
                }else if (ChequeState == 1)
                {
                    Label_CommitMessage.Text = "该支票已经使用";
                }else if (ChequeState == 2)
                {
                    Label_CommitMessage.Text = "该支票已经注销";
                }else{
                    Label_CommitMessage.Text = "";
                    Table_ChequePay.Visible = true;
                }
                if (cf.GetChequeNum() == "")
                {
                    Label_CommitMessage.Text = "支票文件读取失败";
                    return;
                }
                string CmdStr = "";
                DataSet DS;
                if (cf.IsOrdered())
                {
                    CmdStr = @"SELECT RECV_ACC FROM CHEQUE_INFO WHERE CHEQUE_ID=  " + cf.GetChequeNum();
                    DS = Logic.Logic.QueryData(CmdStr);
                    if (DS.Tables[0].Rows.Count != 0)
                    {
                        TextBox_PayeeAccount.Text = DS.Tables[0].Rows[0][0].ToString();
                        TextBox_PayeeAccount.Enabled = false;
                    }
                    else
                    {
                        Label_CommitMessage.Text = "数据库查询失败";
                    }
                }
                // 2010-09-05 Wander: 这里在加载“证书文件”的时候，会报错。崩。
                // cf.GetChequeNum()，返回值为0。
                CmdStr = @"SELECT PASSWORD FROM CHEQUE_INFO WHERE CHEQUE_ID = " + cf.GetChequeNum();
                DS = Logic.Logic.QueryData(CmdStr);
                if (DS.Tables[0].Rows.Count != 0)
                {
                    if (DS.Tables[0].Rows[0][0].ToString() != Security.MD5.HexCompute("", System.Text.Encoding.ASCII))
                        TextBox_PayKey.Visible = true;
                    else
                        TextBox_PayKey.Visible = false;
                }
                else
                {
                    Label_CommitMessage.Text = "数据库查询失败";
                }
                Session["ChequeFileStream"] = CQFileStream;
                ChequeImage.ImageUrl = "ChequeFileImg.aspx";
                if (Label_CommitMessage.Text == "")
                    Table_CommitMessage.Visible = false;
                return;
            }
            ChequeImage.Visible = false;
            Table_CommitMessage.Visible = true;
            Label_CommitMessage.Text = "请选择支票文件";
        }
    }
}
